package cn.e4j.modular.api.service;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.api.mapper.Gwat153Mapper;
import cn.e4j.modular.crm.entity.*;
import cn.e4j.modular.crm.mapper.*;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by wenchuan.li on 2018/6/14.
 */

@Service
public class Gwat153Service {

    @Autowired
    Gwat153Mapper gwat153Mapper;

    @Autowired
    GwakTMapper gwakTMapper;

    @Autowired
    GwaklTMapper gwaklTMapper;

    @Autowired
    GwalTMapper gwalTMapper;

    @Autowired
    GwbeTMapper gwbeTMapper;

    @Autowired
    GwbgTMapper gwbgTMapper;

    @Autowired
    GwbglTMapper gwbglTMapper;

    @Autowired
    GwbhTMapper gwbhTMapper;


    /**
     * 获取商机主题列表
     *
     * @param inDto
     * @return
     */
    public List<Dto> getGwbgPage(Dto inDto) {
        List<Dto> p = new ArrayList<>();

        p = gwat153Mapper.getGwbgPage(inDto);

//        for (Dto o : p) {
//            inDto.put("gwbe001", o.getString("gwbg001"));
//            o.put("details", sqlDao.list(Gwat153Service.class.getName() + ".getGwbeList", inDto));
//
//        }
        return p;
    }

    /**
     * 获取客户信息
     *
     * @param inDto
     * @return
     */
    public List<Dto> getGwakPage(Dto inDto) {
        return gwat153Mapper.getGwakInfo(inDto);
    }


    /**
     * 获取经销商信息
     *
     * @param inDto
     * @return
     */
    public List<Dto> getJxsPage(Page page,Dto inDto) {
        return gwat153Mapper.getJxsInfoPage(page,inDto);
    }

    /**
     * 获取当前的客户编号（经销商）， 用于生成下一个流水号
     *
     * @param inDto
     * @return
     */
    public Dto getGwak001(Dto inDto) {
        return gwat153Mapper.getGwak001(inDto);
    }

    /**
     * 经销商信息保存
     */
    @Transactional
    public void saveJxs(GwakT gwak_tPO, GwaklT gwakl_tPO, GwalT gwal_tPO) {

        gwak_tPO.insert();
        gwakl_tPO.insert();
        gwal_tPO.insert();
    }

    /**
     * 商机主题 保存
     */
    @Transactional
    public Dto gwat153Save(SysUser currentUser,Dto inDto) {
        Dto outDto = new Dto();
        Date date = new Date();
        GwbgT gwbg_tPO = new GwbgT();
        GwbeT gwbe_tPO = new GwbeT();
        GwbglT gwbgl_tPO = new GwbglT();
        String gwbgl001 = DateUtils.getDate("yyyyMMddHHmmssSSS");

        JSONObject inJson = JSONObject.parseObject(inDto.getString("obj"));
        JSONArray orders = inJson.getJSONArray("CHARACTERISTICS");
        if (inJson.getString("GWBGL001").equals("0")) {
            for (int i = 0, j = orders.size(); i < j; i++) {
                JSONObject item = orders.getJSONObject(i);
                gwbe_tPO.setGWBEENT(currentUser.getDBC());
                gwbe_tPO.setGWBE001(gwbgl001);
                gwbe_tPO.setGWBE002(item.getString("GWBE002"));
                gwbe_tPO.setGWBE003(" ");
                gwbe_tPO.setGWBE004(" ");
                gwbe_tPO.setGWBE005(" ");
                gwbe_tPO.setGWBEACTI("Y");
//                gwbe_tPO.setGWBEGRUP(currentUser.getORG_ID());
//                gwbe_tPO.setGWBEORIG(currentUser.getORG_ID());
//                gwbe_tPO.setGWBEORIU(currentUser.getID());

                gwbe_tPO.insert();
            }
            gwbgl_tPO.setGWBGLENT(currentUser.getDBC());
            gwbgl_tPO.setGWBGL001(gwbgl001);
            gwbgl_tPO.setGWBGL002("2");
            gwbgl_tPO.setGWBGL003(inJson.getString("GWBGL003"));
            gwbgl_tPO.setGWBGL004("");//助记码

            gwbg_tPO.setGWBGENT(currentUser.getDBC());
            gwbg_tPO.setGWBG001(gwbgl001);
            gwbg_tPO.setGWBG002(inJson.getString("GWBG002"));
            gwbg_tPO.setGWBG003(inJson.getString("GWBG003"));
            gwbg_tPO.setGWBG004(inJson.getString("GWBG004"));
            gwbg_tPO.setGWBG005(inJson.getString("GWBG005"));

            gwbg_tPO.setGWBGACTI("Y");
//            gwbg_tPO.setGWBGUSER(currentUser.getID());
//            gwbg_tPO.setGWBGGRUP(currentUser.getORG_ID());
//            gwbg_tPO.setGWBGORIU(currentUser.getID());
//            gwbg_tPO.setGWBGORIG(currentUser.getORG_ID());
//            gwbg_tPO.setGWBGDATE(LocalDate.now());
//            gwbg_tPO.setGWBGTIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));

            gwbg_tPO.insert();
            gwbgl_tPO.insert();
        } else {
            //删除可选经销商  后 重新写入
            inDto.put("GWBE001", inJson.getString("GWBGL001"));
            gwat153Mapper.deleteByKey(inDto);
            for (int i = 0, j = orders.size(); i < j; i++) {
                JSONObject item = orders.getJSONObject(i);
                gwbe_tPO.setGWBEENT(currentUser.getDBC());
                gwbe_tPO.setGWBE001(inJson.getString("GWBGL001"));
                gwbe_tPO.setGWBE002(item.getString("GWBE002"));
                gwbe_tPO.setGWBE003(item.getString("GWBE003"));
                gwbe_tPO.setGWBE004(" ");
                gwbe_tPO.setGWBE005(" ");
                gwbe_tPO.setGWBEACTI("Y");
//                gwbe_tPO.setGWBEGRUP(currentUser.getORG_ID());
//                gwbe_tPO.setGWBEORIG(currentUser.getORG_ID());
//                gwbe_tPO.setGWBEORIU(currentUser.getID());
//                gwbe_tPO.setGWBEUSER(currentUser.getID());
//                gwbe_tPO.setGWBEDATE(LocalDate.now());
//                gwbe_tPO.setGWBETIME(DateUtils.getTime());
//                gwbe_tPO.setGWBEMODU(currentUser.getID());

                gwbe_tPO.insert();
            }
            gwbgl_tPO.setGWBGLENT(currentUser.getDBC());
            gwbgl_tPO.setGWBGL001(inJson.getString("GWBGL001"));
            gwbgl_tPO.setGWBGL002("2");
            gwbgl_tPO.setGWBGL003(inJson.getString("GWBGL003"));
            gwbgl_tPO.setGWBGL004("");//助记码

            gwbg_tPO.setGWBGENT(currentUser.getDBC());
            gwbg_tPO.setGWBG001(inJson.getString("GWBGL001"));
            gwbg_tPO.setGWBG002(inJson.getString("GWBG002"));
            gwbg_tPO.setGWBG003(inJson.getString("GWBG003"));
            gwbg_tPO.setGWBG004(inJson.getString("GWBG004"));
            gwbg_tPO.setGWBG005(inJson.getString("GWBG005"));

            gwbg_tPO.setGWBGACTI("Y");
//            gwbg_tPO.setGWBGUSER(currentUser.getID());
//            gwbg_tPO.setGWBGGRUP(currentUser.getORG_ID());
//            gwbg_tPO.setGWBGORIU(currentUser.getID());
//            gwbg_tPO.setGWBGORIG(currentUser.getORG_ID());
//            gwbg_tPO.setGWBGDATE(LocalDate.now());
//            gwbg_tPO.setGWBGTIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));
//            gwbg_tPO.setGWBGMODU(currentUser.getID());

            gwbg_tPO.updateById();
            gwbgl_tPO.updateById();
        }

        outDto.setAppCode(200);
        return outDto;
    }


    /**
     * 根据商机主题编号获取拜访记录
     */
    public int checkTheme(Dto inDto) {
        return gwbhTMapper.wx_rows(inDto);

    }

    /**
     * 根据商机主题
     */
    public int checkExist(Dto inDto) {

        return gwbglTMapper.wx_rows(inDto);
    }

    /**
     * 获取该商机主题对应的经销商列表
     *
     * @param inDto
     * @return
     */
    public List<Dto> jxsList(Dto inDto) {
        return gwat153Mapper.getJxsList(inDto);
    }

    /**
     * 删除商机主题
     *
     * @param inDto
     * @return
     */
    @Transactional
    public void deleteTheme(Dto inDto) {
        String gwbe001 = inDto.getString("GWBE001");
        QueryWrapper<GwbeT> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("GWBE001",gwbe001);
        QueryWrapper<GwbgT> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("GWBG001",gwbe001);
        QueryWrapper<GwbglT> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("GWBGL001",gwbe001);

        new GwbeT().delete(wrapper1);
        new GwbgT().delete(wrapper2);
        new GwbglT().delete(wrapper3);
    }

    /**
     * 获取业务员列表
     *
     * @param inDto
     * @return
     */
    public List<Dto> listUserPage(Dto inDto) {
        return gwat153Mapper.listUserPage(inDto);
    }

}
